//
//  RealNameViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/14.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "RealNameViewController.h"
#import "MessageCenterSubCell.h"
#import "MessageCenterStore.h"

@interface RealNameViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;

@end

@implementation RealNameViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.sectionHeaderHeight = 9.5;
//        _tableView.emptyDataSetDelegate = self;
//        _tableView.emptyDataSetSource = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        _tableView.estimatedRowHeight = 83;
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
    }
    
    return _tableView;
}


- (void)refresh{
    
    MessageCenterStore *store = [[MessageCenterStore alloc] init];
    
    MJWeakSelf
    [store getMessageInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id andType:@"4" andIsRead:NO Success:^(NSArray *arr, BOOL isRead) {
        
        if(isRead){
            
            NSDictionary *dic = [NSDictionary dictionaryWithObject:@"3" forKey:@"info"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"HADNOREADMSG" object:nil userInfo:dic];
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = arr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(0);
        
        make.bottom.equalTo(self.view).offset(-45);
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageCenterSubCell *cell = [MessageCenterSubCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.model = self.listArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
